package com.yc.test.controller;

import com.yc.test.entity.BopDict;
import com.yc.test.entity.BopDictInfo;
import com.yc.test.entity.Dict;
import com.yc.test.entity.File;
import com.yc.test.repository.DocumentRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/dict")
@Api(tags = "Dict", value = "数据字典API", description = "数据字典API")
public class DictController {

    @Autowired
    MongoRepository<File, String> mongoRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private DocumentRepository documentRepository;

    @GetMapping("/test")
    @ApiOperation(response = Object.class, value = "test", notes = "test")
    public Object test() throws Exception {

//        Document queryObject = new Document();
//        queryObject.put("code", "ResourceType");
//
//        Document fieldsObject = new Document();
//        fieldsObject.put("code",true);
//        fieldsObject.put("nameCn",true);
//
//        Query query = new BasicQuery(new Document(), fieldsObject);
//
//        List<BopDict> list = mongoTemplate.find(query, BopDict.class);
//        List<File> list = mongoTemplate.findAll(File.class);
//        List<File> list = mongoRepository.findAll();

//        mongoTemplate.updateFirst(Query.query(Criteria.where("file.oid").is("1062264381040697344")),
//                Update.update("file.url","yc1"),"Document");

        List<BopDict> list = mongoTemplate.findAll(BopDict.class);

        mongoTemplate.insertAll(list);
        mongoTemplate.insert(list.get(0));

        BopDictInfo dictInfo = new BopDictInfo();
        BeanUtils.copyProperties(list.get(0), dictInfo);
        return dictInfo;
    }

    @GetMapping("/testId")
    @ApiOperation(response = Object.class, value = "testId", notes = "testId")
    public Object testId() throws Exception {
        Dict dict = new Dict();
        dict.setCode("yc");
        dict.setFileList(new ArrayList<File>() {{
            add(new File("yc1"));
            add(new File("yc2"));
        }});
        mongoTemplate.insert(dict);
        return dict;
    }
}
